package com.pengtu.gsj.service.impl.yh;

import java.text.ParseException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.yh.TrainingDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.yh.Training;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.service.yh.TrainingService;
import com.pengtu.gsj.utils.DateUtils;

/**
 * 
 * 
 * TrainingServiceImpl
 * 
 * 2017年4月18日 下午4:21:30
 * 
 * @version 1.0.0
 * 
 */
@Transactional
@Service
public class TrainingServiceImpl implements TrainingService {

	@Autowired
	private TrainingDao trainingDao;
	
	@Autowired
	private SystemManageService systemManageService;

	@Override
	public List<Training> getTrainings(QueryFilter queryFilter) {
		return trainingDao.find(queryFilter);
	}

	@Override
	public Page<Training> findTrainings(Page<Training> page,
			QueryFilter queryFilter) {
		return trainingDao.findPage(page, queryFilter);
	}

	@Override
	public Training getTraining(String id) {
		return trainingDao.get(id);
	}

	@Override
	public void saveTraining(Training training) {
		trainingDao.save(training);
	}

	@Override
	public void deleteTraining(String id) {
		trainingDao.delete(id);

	}

	@Override
	public void deleteTraining(Training training) {
		if (training.getAttachments() != null) {
			for (Attachment attachment : training.getAttachments()) {
				systemManageService.deleteAttachment(attachment);
			}
		}
		trainingDao.delete(training);
	}

	/**
	 * 查询满足国省检时间段的培训记录
	 * @throws ParseException 
	 */
	@Override
	public List<Training> getCheckTrains(String startTime, String endTime) throws ParseException {
		QueryFilter queryFilter = new QueryFilter();
		if (StringUtils.isNotEmpty(startTime) && DateUtils.isDate(startTime)) {
			queryFilter.addFilter("trainStartTime", DateUtils.convertStringToDate(startTime+"-01-01"), MatchType.GE);
		}
		if (StringUtils.isNotEmpty(endTime) && DateUtils.isDate(endTime)) {
			queryFilter.addFilter("trainEndTime",  DateUtils.convertStringToDate(endTime+"-12-31"), MatchType.LE);
		}
		return trainingDao.find(queryFilter);
	}

}
